﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Xml;
/// <summary>
/// Ratingrecord 的摘要说明
/// </summary>
public class Ratingrecord
{
    private string _ApplicationPath;//应用程序的字符串
    private DataSet _ds;
	public Ratingrecord()
	{
		//
		// TODO: 在此处添加构造函数逻辑
		//
	}
    
    protected string Rootpath//检索目录
    {
        get
        {
            if (_ApplicationPath != null)
                return _ApplicationPath;
            else
                if (HttpContext.Current != null)
                    return _ApplicationPath = HttpContext.Current.Server.MapPath(HttpContext.Current.Request.ApplicationPath);
   else
                {
                    throw new ApplicationException("无法发现根目录！");
                }
        }
    }
    protected string XmlFile //和xml文件里面的路径合并
    {
        get { return Path.Combine(Rootpath, @"App_Data\RatingRecord.xml"); }

    }
    public string Count(string clas, string stars)
    {
        Table.DefaultView.RowFilter = "class='"+clas +"'and stars='"+stars+"'";
        return Table.DefaultView.Count.ToString();
    }
    private DataTable Table
    {
        get
        {
            if (_ds == null)
            {
                _ds = new DataSet();
                _ds.ReadXml(XmlFile);
            }
            return _ds.Tables[0];
        }
       
    }
    public void Insert(string clas, string stars)
    {
        DataRow dr = Table.NewRow();//新的一行
        dr["id"] = Table.Rows.Count;
        dr["class"] = clas;
        dr["stars"] = stars;
        dr["time"] = DateTime.Now.ToLocalTime();
        Table.Rows.Add(dr);
        SaveChances();//保存插入结果
    }

    protected void SaveChances()//保存XML文件
    {
        _ds.WriteXml(XmlFile, XmlWriteMode.IgnoreSchema);
        //XmlDocument doc = new XmlDocument();
        //doc.LoadXml(_ds.GetXml());
        //doc.Save(XmlFile);
    }
}
